<?php defined('SYSPATH') or die('No direct script access.');
/**
 * 管理员权限资源管理
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: privilege.php 1 2014-01-13 15:05:51Z zhouyl $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
class Controller_Admin_Manager_Privilege extends Controller_Admin_Base
{

    /**
     * 资源列表
     */
    public function action_index()
    {
        $this->privilege('manager.privilege.list');

        $this->view->res_id = (int) $this->request->query('res_id');
        $this->view->priv_name = (string) $this->request->query('priv_name');
        $this->view->token = (string) $this->request->query('token');

        $autosort = $this->autosort('priv.sort_order', 'asc');

        $sql = Model::factory('manager_privilege')->all_sql()
            ->order_by($autosort->field, $autosort->direct)
            ->order_by('priv.priv_id', 'ASC');

        if ($this->view->res_id)
        {
            $sql->where('res.res_id', '=', $this->view->res_id);
        }

        if ($this->view->priv_name)
        {
            $sql->where('priv.priv_name', 'LIKE', '%'.$this->view->priv_name.'%');
        }

        if ($this->view->token)
        {
            $sql->where('priv.token', 'LIKE', '%'.$this->view->token.'%');
        }

        $this->view->pagination = new Pager($sql);
        $this->view->resources = Model::factory('manager_resource')->all();
    }

    /**
     * 添加资源
     */
    public function action_add()
    {
        $this->privilege('manager.privilege.add');

        $model = Model::factory('manager_privilege');

        if ($this->request->is_post())
        {
            if ( ! $model->add($this->request->post()))
            {
                $this->view->errors = $model->errors('admin.manager.privilege');
            }
            $this->render('admin/common/message');
        }

         $this->view->resources = Model::factory('manager_resource')->all();
    }

    /**
     * 编辑资源
     */
    public function action_edit()
    {
        $this->privilege('manager.privilege.edit');

        $priv_id = $this->request->param('id');
        $model = Model::factory('manager_privilege');
        $privilege = $model->get($priv_id);

        if ( ! $privilege)
        {
            $this->message('admin.manager.privilege.not_found')
                ->link(URL::site('admin/manager_privilege'), 'admin.manager.privilege.index_url')
                ->render();
        }

        if ($this->request->is_post())
        {
            if ( ! $model->edit($priv_id, $this->request->post()))
            {
                $this->view->errors = $model->errors('admin.manager.privilege');
            }
            $this->render('admin/common/message');
        }

        $this->view->set($privilege);
        $this->view->resources = Model::factory('manager_resource')->all();
    }

    /**
     * 删除资源
     */
    public function action_del()
    {
        $this->privilege('manager.privilege.del');

        $priv_id = $this->request->param('id');
        $model = Model::factory('manager_privilege');

        if ( ! $model->del($priv_id))
        {
            $this->view->errors = $model->errors('admin.manager.privilege');
        }

        $this->render('admin/common/message');
    }

}
